package com.tianqu.tms.entity;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.TmsEntity;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.dao.CarImageDao;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dto.CarImgDto;

/**
 * 车辆图片信息entity
 * 
 * @author tianshifu
 * 
 */
public class CarImage extends TmsEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3347344983471093865L;

	private int cId;// 车辆ID

	private String imgType;// 图片类型

	private String imgKey;// 图片键值

	private String imgPath;// 图片路径
    private int imgSrc; //车辆来源
	public static CarImage getInstance() {
		CarImage entity = new CarImage();
		entity.setExisting(false);
		return entity;
	}

	public CarImgDto setEntityToDto(CarImage entity) {

		CarImgDto dto = new CarImgDto();
		dto.setImgKey(entity.getImgKey());
		dto.setImgType(entity.getImgType());
		dto.setImgPath(dto.getImgPath());
		return dto;
	}

	/**
	 * 根据车辆ID获取该车辆所有图片信息
	 * 
	 * @return
	 * @throws TmsDaoException
	 */
	public List<CarImage> getCarImgInfoBySerial(int cId) throws TmsDaoException {

		List<CarImage> list = new ArrayList<CarImage>();
		CarImage entity = new CarImage();
		CarImageDao dao = (CarImageDao) TmsDao.getInstance(TmsDao.TMS_PIC);
		List<CarImgDto> dtoList = dao.findAllCarImgInfoBySerial(cId);
		for (int i = 0; i < dtoList.size(); i++) {
			CarImgDto dto = (CarImgDto) dtoList.get(i);
			entity = setEntityFromDto(dto);
			list.add(entity);
		}
		return list;
	}
	
	/**
	 * 根据车辆ID获取该车辆所有图片信息 来源为3
	 * 
	 * @return
	 * @throws TmsDaoException
	 */
	public List<CarImage> getCarImgInfoByCId3(int cId) throws TmsDaoException {

		List<CarImage> list = new ArrayList<CarImage>();
		CarImage entity = new CarImage();
		CarImageDao dao = (CarImageDao) TmsDao.getInstance(TmsDao.TMS_PIC);
		List<CarImgDto> dtoList = dao.getCarImgInfoByCId3(cId);
		for (int i = 0; i < dtoList.size(); i++) {
			CarImgDto dto = (CarImgDto) dtoList.get(i);
			entity = setEntityFromDto(dto);
			list.add(entity);
		}
		return list;
	}
	
	/**
	 * 根据图片的type值获得车辆图片信息
	 * @param id
	 * @param imgType
	 * @return
	 * @throws TmsDaoException
	 */
	public List<CarImage> getCarImage(int cid,int src, int imgType)throws TmsDaoException{
		List<CarImage> list = new ArrayList<CarImage>();
		CarImage entity = new CarImage();
		CarImageDao dao = (CarImageDao) TmsDao.getInstance(TmsDao.TMS_PIC);
		List<CarImgDto> dtoList = dao.getCarImage(cid,src,imgType);
		for (int i = 0; i < dtoList.size(); i++) {
			CarImgDto dto = (CarImgDto) dtoList.get(i);
			entity = setEntityFromDto(dto);
			list.add(entity);
		}
		return list;
	}
	
	
	
     /**
      * 插入图片信息
      * @param img
      * @throws TmsDaoException
      */
	public void insertCarImages(List<CarImage> img) throws TmsDaoException {
		CarImageDao dao = (CarImageDao) TmsDao.getInstance(TmsDao.TMS_PIC);
		List<CarImgDto> imge = new ArrayList<CarImgDto>();
		for (CarImage carIm : img) {
			CarImgDto carImage = new CarImgDto();
			carImage.setcId(carIm.getcId());
			carImage.setImgType(carIm.getImgType());// 图片类型
			carImage.setImgKey(carIm.getImgKey());// 图片键值
			carImage.setImgPath(carIm.getImgPath());// 图片路径
			carImage.setImgSrc(carIm.getImgSrc());//来源
			imge.add(carImage);
		}

		dao.insertCarImageInfo(imge);

	}
	
	/**
	 * 更新图片信息
	 * @param img
	 * @throws TmsDaoException
	 */
    public void updateImage(CarImage img)throws TmsDaoException{
    	CarImgDto carImage = new CarImgDto();
    	carImage.setImgType(img.getImgType());// 图片类型
    	carImage.setImgKey(img.getImgKey());// 图片键值
    	carImage.setImgPath(img.getImgPath());// 图片路径
    	carImage.setcId(img.getcId());//车辆ID
    	carImage.setImgSrc(img.getImgSrc());
    	CarImageDao dao = (CarImageDao) TmsDao.getInstance(TmsDao.TMS_PIC);
    	dao.updateImage(carImage);
    }
    
    /**
     * 查询单张图片信息
     * @param cId
     * @param imgKey
     * @return
     * @throws TmsDaoException
     */
    public CarImage findDtoByIdAndKey(int cId, String imgKey)throws TmsDaoException{
    	CarImageDao dao = (CarImageDao) TmsDao.getInstance(TmsDao.TMS_PIC);
    	CarImgDto carImage= dao.findDtoByIdAndKey(cId, imgKey);
    	if(carImage==null)return null;
    	CarImage carimg = new CarImage();
    	carimg.setImgType(carImage.getImgType());// 图片类型
    	carimg.setImgKey(carImage.getImgKey());// 图片键值
    	carimg.setImgPath(carImage.getImgPath());// 图片路径
    	carimg.setcId(carImage.getcId());//车辆ID
    	return carimg;
    	
    }
    /**
	 * 根据车辆id、图片类型、图片key值取得图片路径
	 */
    public String imgPath(int cid,String imgType,String imgKey,int imgsrc) throws TmsDaoException{
    	CarImageDao dao = (CarImageDao) TmsDao.getInstance(TmsDao.TMS_PIC);
    	return dao.imgPath(cid,imgType,imgKey,imgsrc);
    }
    

	/**
	 * 将DTO对象放入entity中
	 * 
	 * @return
	 */
	public CarImage setEntityFromDto(CarImgDto dto) {
		CarImage entity = new CarImage();
		entity.setImgPath(dto.getImgPath());
		entity.setImgKey(dto.getImgKey());
		entity.setImgType(dto.getImgType());
		return entity;
	}

	public int getcId() {
		return cId;
	}

	public void setcId(int cId) {
		this.cId = cId;
	}

	public String getImgType() {
		return imgType;
	}

	public void setImgType(String imgType) {
		this.imgType = imgType;
	}

	public String getImgKey() {
		return imgKey;
	}

	public void setImgKey(String imgKey) {
		this.imgKey = imgKey;
	}

	public String getImgPath() {
		return imgPath;
	}

	public void setImgPath(String imgPath) {
		this.imgPath = imgPath;
	}

	public int getImgSrc() {
		return imgSrc;
	}

	public void setImgSrc(int imgSrc) {
		this.imgSrc = imgSrc;
	}

	@Override
	public void save() throws TmsDaoException, TmsException {

	}

}
